package systemPackage;
import guiPackage.GUI_MainWindow;

import java.util.ArrayList;
import java.util.List;

import adapterPackage.LimanAdapter;
import adapterPackage.SeferAdapter;
import adapterPackage.UlkeAdapter;

import dataConnectionPackage.DynamicDataSource;
import dataConnectionPackage.Facade;
import dataTypesPackage.DistanceBetweenLiman;
import dataTypesPackage.Guzergah;
import dataTypesPackage.Liman;
import dataTypesPackage.Musteri;
import dataTypesPackage.Satis;
import dataTypesPackage.Sefer;
import dataTypesPackage.Ulke;


public abstract class MainSystem {
	static int LANGUAGE = 0;			//0 for english, 1 for turkish
	static int DATABASECONNECTION = 0;	//0 for dynamic lists, 1 for mysql
	static DynamicDataSource dynamicDataSource = new DynamicDataSource();
	static GUI_MainWindow GUImainWindow;
	
	/**
	 * @param args
	 */
	public static void set(Object objectToStore, Object objectClass){
		Facade.getInstance().set(objectToStore, objectClass);
	}
	
	public static Object get(int ID, Object objectClass) {
		// used for feeding GUI elements with objects
		return Facade.getInstance().get(ID, objectClass);
	}
	
	public static void main(String[] args) {
		// The main function of the project.
		LocalStrings.populateLocalStrings();			//this must be called every run to load strings.
		dynamicDataSource.populateDatas();
		GUImainWindow = new GUI_MainWindow();
		GUImainWindow.setVisible(true);
	}

	public static void editMusteri(Musteri musteri) {
		// edits given client
		dynamicDataSource.editMusteri(musteri);
	}

	public static List<Object> getAll(Object objectClass) {
		// used for feeding GUI elements with the all objects of given class
		return Facade.getInstance().getAll(objectClass);
	}

	public static Ulke getUlkeByName(String ulkeName) {
		// returns Ulke object that matches the given name
		List<Ulke> ulkes = UlkeAdapter.getAllulke();
		for(Ulke ulke:ulkes){
			if (ulke.getName().equals(ulkeName) )
				return ulke;
		}
		return null;
	}

	public static void saveNewLiman(Liman liman, List<DistanceBetweenLiman> distanceBetweenLimans) {
		// saves given Port via data connector.
		set(liman, Liman.class);
		//	dynamicDataSource.storeNewLiman(liman, distanceBetweenLimans);			//sends a null distance list to dataconnector
	}

	
	public static boolean isFirstLiman() {
		// if the system has no port records returns true
		return (LimanAdapter.getAllLiman().size() == 0);
	}
	
	public static int getLimanCount() {
		// returns the size of the Limanlist.
		return LimanAdapter.getAllLiman().size();
	}

	public static List<DistanceBetweenLiman> getAllDistances() {
		// This method returns all the distancesBetweenLimans.
		return dynamicDataSource.getAllDistancesBetweenLiman();
	}

	public static List<Sefer> getSeferbyGuzergah(Guzergah guzergah) {
		// returns sefer list that matches the given guzergah
		List<Sefer> seferList = SeferAdapter.getAllSefer();
		List<Sefer> matchedSeferList = new ArrayList<Sefer>();
		for(Sefer s:seferList){
			if (s.getGuzergah() == guzergah )
				matchedSeferList.add(s);
		}
		return matchedSeferList;
		
	}

	public static void saveNewSatis(Satis satis) {
		// This method stores the satis.
		dynamicDataSource.storeNewSatis(satis);
		
	}
	
	public static void changeLastTransaction(String lastTransaction){
		GUImainWindow.updateLastTransaction(lastTransaction);
	}

}
